//
//  NewDiscussionSource.m
//  HiQ
//
//  Created by arvintan pang on 12-7-31.
//  Copyright (c) 2012年 __MyCompanyName__. All rights reserved.
//

#import "NewDiscussionSource.h"
#import "Buddy.h"
#import "BuddyListSource.h"
#import "MainWindowController.h"
#import "ImageAndTextCell.h"
#import "ClusterListSource.h"

static NewDiscussionSource *shareNewDiscussion;

@implementation NewDiscussionSource
@synthesize _selectedArray;
@synthesize _delegate;

+(id) shareNewDiscussion  
{
    return shareNewDiscussion;
}

-(id) init
{
    _connection = [Connection sharedServerConnection];
    id pdc = [[PacketDispathCenter alloc]init];
    //这里为了去除warning，以后再改
    (void)pdc;
    shareNewDiscussion = self;
    
    _selectedArray = [[NSMutableArray alloc] initWithCapacity:20];
    Buddy *myself = [MainWindowController shareMyselfInfo];
    [_selectedArray addObject:myself];
    _delegate = [MainWindowController sharedMainWindowController];
//    [_selectedArray addObject:buddy];
    return [super init];
}

-(NSInteger) numberOfRowsInTableView:(NSTableView*) tv 
{
    return [_selectedArray count];
}

-(id) tableView:(NSTableView *)tableView objectValueForTableColumn:(NSTableColumn *)tableColumn row:(NSInteger)row
{
      return nil;
}



-(NSInteger) selectedNum
{
    return [_selectedArray count];
}

-(void) addMember:(id) member
{
    BOOL isMulti = NO;
    for (NSUInteger i = 0; i < [_selectedArray count]; ++i) {
        if (member == [_selectedArray objectAtIndex:i])
            isMulti = YES;
    }
    if(!isMulti)
    {
        [_selectedArray addObject:member];
    }
}


-(void) removeMembersAtIndexs:(NSIndexSet *)rows
{
    [_selectedArray removeObjectsAtIndexes:rows];
}

-(void) sendNewDiscussionMsg:(NSString*) discussionName HeadIamge:(NSString *)headImage
{
    NSUInteger userID = [[MainWindowController shareMyselfInfo] buddyQQ];
    NSData *discussionNameData = [discussionName dataUsingEncoding:NSUTF8StringEncoding];
    NSData *imageData = [headImage dataUsingEncoding:NSUTF8StringEncoding];
    NSUInteger count = [_selectedArray count]; 
    NSUInteger nameLen = [discussionNameData length];
    NSUInteger imageLen = [imageData length];
    
    CLUSTERCREATEHRD *createHrd = malloc(sizeof(CLUSTERCREATEHRD));
    createHrd->userID = userID;
    createHrd->nameLen = nameLen;
    createHrd->headImageLen = imageLen;
    createHrd->count = count;
    NSMutableData *data = [[NSMutableData alloc] initWithBytes:createHrd length:sizeof(CLUSTERCREATEHRD)];

    [data appendData:discussionNameData];
    [data appendData:imageData];
    
    for (NSUInteger i = 0; i < [_selectedArray count]; ++i) {
        Buddy *buddy = [_selectedArray objectAtIndex:i];
        NSUInteger userID = buddy.buddyQQ;
        [data appendBytes:&(userID) length:sizeof(NSUInteger)];
    }
    
    [_connection sendData:data cmd:CMD_FUN_GROUP subcmd:CMD_GROUP_CREATE_REQUEST];
}

-(void) onReceiveNewDiscussionPacket:(NSData *)data
{
    NSUInteger subFun = *((NSUInteger*)([data bytes] + sizeof(NSUInteger)));
    if (subFun == CMD_GROUP_CREATE_YES)
    {
        void *bytes = (void*)([data bytes] + 2*sizeof(NSUInteger));
        
        CLUSTERCREATEREPLYHRD *replyHrd = (CLUSTERCREATEREPLYHRD*)bytes;
        NSUInteger groupID = replyHrd->groupID;
        NSUInteger nameLen = replyHrd->nameLen;
        NSUInteger iamgeLen = replyHrd->headImageLen;
        NSString *name = [[NSString alloc] initWithBytes:(bytes + sizeof(CLUSTERCREATEREPLYHRD)) length:nameLen encoding:NSUTF8StringEncoding];
        NSString *headImage = [[NSString alloc] initWithBytes:(bytes + sizeof(CLUSTERCREATEREPLYHRD)+nameLen) length:iamgeLen encoding:NSUTF8StringEncoding];
//        NSData *memberIDData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERCREATEREPLYHRD)+nameLen) length:memberIDLen];
//        NSData *memberNameData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERCREATEREPLYHRD)+nameLen+memberIDLen) length:memberNameLen];
//        NSData *memberImageData = [[NSData alloc] initWithBytes:(bytes+sizeof(CLUSTERCREATEREPLYHRD)+nameLen+memberIDLen+memberNameLen) length:memberImageLen];
        
//        NSArray *memberIDArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberIDData];
//        NSArray *memberNameArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberNameData];
//        NSArray *memberImageArray = [NSKeyedUnarchiver unarchiveObjectWithData:memberImageData];

        Cluster *cluster = [[Cluster alloc] init];
        cluster->clusterId = groupID;
        cluster->clusterName = name;
        cluster->photo = headImage;
        NSMutableArray *groupArray = [[[MainWindowController sharedMainWindowController] clusterSource] clusterGroupArray];
        ClusterGroup *discussionGroup = [groupArray objectAtIndex:0];
        [discussionGroup->clusterList addObject:cluster];
        dispatch_async(dispatch_get_main_queue(), ^{
            [[[MainWindowController sharedMainWindowController] clusterOutlineView] reloadData];
        });
    }

    if(subFun == CMD_GROUP_CREATE_NO)
    {
        NSLog(@"new discussion create failed");
    }
}

@end
